/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2023-11-22 23:42:14
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-11-22 23:50:47
 * @FilePath: \interview\巧用js\mySetTimeout.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mySetTimeout=(callback=()=>{},delay=0)=>{
    let hasRun=false;
    let leftTime=delay;
    let timer=window.setTimeout(()=>{
        hasRun=true;
        callback();
    },leftTime);
    let startTime=+new Date();
    return{
        pause:()=>{
            if(hasRun){
                return;
            }
            const currentTime=+new Date();
            leftTime=leftTime-(currentTime-startTime);
            window.clearTimeout(timer);
        },
        continue:()=>{
            if(hasRun){
                return;
            }
            startTime=+new Date();
            timer=window.setTimeout(()=>{
                hasRun=true;
                callback();
            },leftTime);
        }
    }
}

mySetTimeout(()=>{console.log("到点后操作")},2000);
const timer=mySetTimeout(()=>{},1000);timer.pause();
const timer1=mySetTimeout(()=>{},1000);timer1.continue();